/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.nubotech.gwt.oss.client.util;


/**
 *
 * @author jonnakkerud
 */
public class FileUtils {
    
    public static String getContentType(String fileName) {
        int dot = fileName.indexOf('.');

        if (dot == -1) {
            return "application/octet-stream";
        }
        String extension = fileName.substring(fileName.lastIndexOf('.') + 1);

        String mimeType = MimeType.lookup(extension);
        if (mimeType == null) {
            // try native
            mimeType = nativeLookup(extension);
        }

        if (mimeType != null) {
            return mimeType;
        }
        return "application/octet-stream";
    }

    // Note: ie should not return anything
    private static native String nativeLookup(String ext)  /*-{
        for (i=0; i < $wnd.navigator.mimeTypes.length; i++) {    
            var ar = $wnd.navigator.mimeTypes[i].suffixes.split(",");
            for (j = 0; j < ar.length; j++) {
                if (ar[j] === ext) {
                    return $wnd.navigator.mimeTypes[i].type;
                }
            }
        }
        return null;
    }-*/;
}
